package com.example.greentrack;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BehaviorActivity extends AppCompatActivity {

    private ListView listView;
    private TextView tvTotalCo2;
    private TextView tvSelectedItems;
    private Button btnSubmit;
    private Set<String> selectedBehaviors = new HashSet<>();
    private double totalCo2 = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_behavior);

        // 初始化视图
        listView = findViewById(R.id.lv_behaviors);
        tvTotalCo2 = findViewById(R.id.tv_total_co2);
        tvSelectedItems = findViewById(R.id.tv_selected_items);
        btnSubmit = findViewById(R.id.btn_submit);

        // 设置标题
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("选择今日环保行为\n(每日自动清零)");

        // 行为数据
        final List<String> behaviors = Arrays.asList(
                "步行上班 - 节省0.5kg CO₂",
                "自带咖啡杯 - 节省0.1kg CO₂",
                "关闭未用电器 - 节省1.2kg CO₂",
                "回收利用 - 节省0.3kg CO₂",
                "素食一餐 - 节省0.8kg CO₂"
        );

        // 使用多选列表适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_multiple_choice,
                behaviors
        );

        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        // 列表项点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = behaviors.get(position);
                if (listView.isItemChecked(position)) {
                    selectedBehaviors.add(item);
                    // 修正后的CO2数值提取逻辑
                    String[] parts = item.split("节省|kg");
                    if (parts.length >= 2) {
                        try {
                            totalCo2 += Double.parseDouble(parts[1].trim());
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    selectedBehaviors.remove(item);
                    // 修正后的CO2数值提取逻辑
                    String[] parts = item.split("节省|kg");
                    if (parts.length >= 2) {
                        try {
                            totalCo2 -= Double.parseDouble(parts[1].trim());
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                }
                updateUI();
            }
        });

        // 提交按钮点击事件
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回MainActivity并传递数据
                Intent resultIntent = new Intent();
                resultIntent.putExtra("total_co2", totalCo2);
                setResult(RESULT_OK, resultIntent);
                finish();
            }
        });
    }

    private void updateUI() {
        tvTotalCo2.setText(String.format("今日累计节省: %.1fkg CO₂", totalCo2));

        StringBuilder selectedText = new StringBuilder("今日选择:\n");
        for (String behavior : selectedBehaviors) {
            selectedText.append("✓ ").append(behavior).append("\n");
        }
        tvSelectedItems.setText(selectedText.toString());
    }
}